<?php

namespace frontend\models\code;
use backend\models\system\SysConfig;

use Yii;

/**
 * This is the model class for table "edu_code".
 *
 * @property integer $code_rand
 * @property string $email_phone
 * @property integer $set_time
 */
class Code extends \yii\db\ActiveRecord
{
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'edu_code';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['code_rand', 'email_phone', 'set_time'], 'required'],
            [['code_rand', 'set_time'], 'integer'],
            [['email_phone'], 'string', 'max' => 255]
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'code_rand' => 'Code Rand',
            'email_phone' => 'Email Phone',
            'set_time' => 'Set Time',
        ];
    }
	/*
	* 发送验证码
	*/
	public function getCode($email_phone)
	{
		$sysconfig = new SysConfig();
		$old_code = $this->find()->where(['email_phone'=>$email_phone])->asarray()->one();
		if(!empty($old_code)){
			if(time()-$old_code['set_time']>600){
				$this->deleteAll(['email_phone'=>$email_phone]); 
			}else{
				$code_rand = $old_code['code_rand'];
			}
		}
		if(preg_match("/^([0-9A-Za-z\\-_\\.]+)@([0-9a-z]+\\.[a-z]{2,3}(\\.[a-z]{2})?)$/i",$email_phone)){
			$sysconfig->config_key = "email_title";
			$email_title = $sysconfig->getValueBykey();
			$mail= Yii::$app->mailer->compose();   
			$mail->setTo("$email_phone");  
			$mail->setSubject($email_title['config_value']);
			if(!isset($code_rand)){
				$code_rand = rand(111111,999999);
				$this->code_rand=$code_rand;
				$this->email_phone=$email_phone;
				$this->set_time=time();
				$this->save();
			}
			$sysconfig->config_key = "email_content";
			$email_content = $sysconfig->getValueBykey();
			$content = str_replace("{{verifyurl}}",$code_rand,$email_content['config_value']);
			$mail->setHtmlBody("$content");    //发布可以带html标签的文本		
			if($mail->send()) { 
				
				return 1;die;
			}  
			else  {
				return 2;   
				die(); }
		}else if(preg_match("/1[34587]{1}\d{9}$/",$email_phone)){
			if(!isset($code_rand)){
				$code_rand = rand(111111,999999);
				$this->code_rand=$code_rand;
				$this->email_phone=$email_phone;
				$this->set_time=time();
				$this->save();
			}
			$sysconfig->config_key = "phone_content";
			$phone_content = $sysconfig->getValueBykey();
			$content = str_replace("{{verifyurl}}",$code_rand,$phone_content['config_value']);
			$curlPost = "account=cf_bwedu&password=education123&mobile=".$email_phone."&content=".rawurlencode("$content");
			$url = "http://106.ihuyi.cn/webservice/sms.php?method=Submit";
			$curl = curl_init();
			curl_setopt($curl, CURLOPT_URL, $url);
			curl_setopt($curl, CURLOPT_HEADER, false);
			curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
			curl_setopt($curl, CURLOPT_NOBODY, true);
			curl_setopt($curl, CURLOPT_POST, true);
			curl_setopt($curl, CURLOPT_POSTFIELDS, $curlPost);
			$return_str = curl_exec($curl);
			curl_close($curl);
			return 1;
		}else{
			return 3;
		}
	}
	/*
	*	获取用户验证码
	*/
	public function getUsercode($email_phone)
	{
		$re = $this->find()->where(['email_phone'=>$email_phone])->asarray()->one();
		if(time()-$re['set_time']>600){
			return '';
		}else{
			return $re['code_rand'];
		}	
	}
}
